package sfeir.authform.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import sfeir.authform.client.dao.AuthUser;
import sfeir.authform.client.service.AuthGWTService;
import sfeir.authform.server.authservice.AuthService;
import sfeir.authform.server.authservice.AuthServiceFactory;
import sfeir.authform.server.authservice.standard.AuthStandard;

public class AuthGWTServiceImpl extends RemoteServiceServlet implements AuthGWTService {

    private static final long serialVersionUID = -1630068916302503410L;

    public AuthGWTServiceImpl() {
        super();
    }

    @Override
    public Boolean deleteUser(AuthUser user) {
        return new AuthStandard(getSession()).deleteUser(user);
    }

    @Override
    public Integer getAuthType() {
        return AuthServiceFactory.getAuthService(getSession()).getAuthType();
    }

    @Override
    public AuthUser getCurrentUser() {
        return AuthServiceFactory.getAuthService(getSession()).getCurrentUser();
    }

    @Override
    public String getLoginURL() {
        return AuthServiceFactory.getAuthService(getSession()).createLoginURL("");
    }

    @Override
    public String getLogoutURL() {
        return AuthServiceFactory.getAuthService(getSession()).createLogoutURL("");
    }

    @Override
    public Boolean isUserAdmin() {
        return AuthServiceFactory.getAuthService(getSession()).isUserAdmin();
    }

    @Override
    public Boolean isUserLoggedIn() {
        return AuthServiceFactory.getAuthService(getSession()).isUserLoggedIn();
    }

    @Override
    public AuthUser login(String login, String password) {
        return new AuthStandard(getSession()).login(login, password);
    }

    @Override
    public void logout() {
        new AuthStandard(getSession()).logout();
    }

    @Override
    public Boolean signup(AuthUser user) {
        return new AuthStandard(getSession()).signup(user);
    }
    
    public Boolean getLostPassword(String login){
        return new AuthStandard(getSession()).getLostPassword(login);
    }

    @Override
    public Boolean updateUser(AuthUser user) {
        return new AuthStandard(getSession()).updateUser(user);
    }
    
   /**
     * Define here the parameters for the sending of email to your users. This
     * will be used for the retrieving of passwords.
     * 
     * @param from
     *            The email address of one of the developpers of your AppEngine
     *            application.<br/>
     *            Most of the time an address is created with the domain name,
     *            and added to the developpers list in the appengine
     *            administration panel. <br/>
     *            Thus to be used just for communication with users.
     * @param subject The subject of the mail.
     * @param bodyBefore The whole part of the body to be written before the password.
     * @param bodyAfter The part of the body to be written after the password.
     */
    public void setMailPasswordSettings(String from, String subject, String bodyBefore, String bodyAfter) {
        new AuthStandard(getSession()).setMailPasswordSettings(from, subject, bodyBefore, bodyAfter);
        
    }
    
    private HttpSession getSession() {
        HttpServletRequest request = getThreadLocalRequest();
        return request.getSession(true);
    }
}
